/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.lcpgenerator.method;

import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.bef.dtgenerator.core.rtgen.lcpgenerator.LcpClassGeneratorContext;
import com.inspur.edp.bef.spi.extend.IBEManagerExtend;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.QualifiedName;
import org.eclipse.jdt.core.dom.ReturnStatement;

public class LcpAddExtendsGenerator extends ClassMethodGenerator {

  private final LcpClassGeneratorContext parentCtx;

  public LcpAddExtendsGenerator(LcpClassGeneratorContext parentCtx) {
    this.parentCtx = parentCtx;
  }

  private BefCoreGenCtx getBefCoreGenCtx()
  {
    return parentCtx.getProjContext();
  }
  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();

    MethodInvocation asListInvocation = ast.newMethodInvocation();
    asListInvocation.setName(ast.newSimpleName("asList"));
    QualifiedName name = ast.newQualifiedName(
        ast.newQualifiedName(
            ast.newSimpleName("java"),
            ast.newSimpleName("util")),
        ast.newSimpleName("Arrays"));
    asListInvocation.setExpression(name);

    for(MediateType mediateType:getBefCoreGenCtx().getBefManagerGenContext().getExtendProviders())
    {
      addImport(mediateType.getFullName());
      ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
      classInstanceCreation.setType(new TypeInfo(mediateType.getFullName()).getType(ast));
      MethodInvocation getConfigIdInvocation = ast.newMethodInvocation();
      getConfigIdInvocation.setName(ast.newSimpleName("getBEType"));
      classInstanceCreation.arguments().add(getConfigIdInvocation);
      asListInvocation.arguments().add(classInstanceCreation);
    }

//    for (CefMgrTypeInfo mgrType : parentCtx.getProjContext().getMgrTypes()) {
//      ExtendProvidersAttribute attr = (ExtendProvidersAttribute) ReflectionUtils
//          .getAnnotation(mgrType.getImplType(), ExtendProvidersAttribute.class);
//      if (attr == null || attr.value() == null || attr.value().length == 0) {
//        continue;
//      }
//      for(Class item : attr.value()){
//        addImport(getBefCoreGenCtx().getBefManagerGenContext().getExtendProviders());
//        ClassInstanceCreation classInstanceCreation=ast.newClassInstanceCreation();
//        classInstanceCreation.setType(new TypeInfo(item).getType(ast));
//        MethodInvocation getConfigIdInvocation = ast.newMethodInvocation();
//        getConfigIdInvocation.setName(ast.newSimpleName("getBEType"));
//        classInstanceCreation.arguments().add(getConfigIdInvocation);
//        asListInvocation.arguments().add(classInstanceCreation);
//      }
//    }
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(asListInvocation);
    block.statements().add(returnStatement);
    return block;
  }

  private static final String ItemName = "getExtends";

  @Override
  protected String getMethodName() {
    return ItemName;
  }

  @Override
  protected TypeInfo getReturnType() {
    addImport(IBEManagerExtend.class.getTypeName());//??????泛型不加引用
    TypeInfo typeInfo = new TypeInfo(List.class.getTypeName());
    typeInfo.setIsParameterizedType(true);
    typeInfo.getArguments().add(new TypeInfo(IBEManagerExtend.class));
    return typeInfo;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifiers() {
    ArrayList<ModifierKeyword> list = new ArrayList();
    list.add(ModifierKeyword.PROTECTED_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return null;
  }
}
